﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBOffice.DAL;
using System.Data.Entity;
using System.Data;


namespace PBOffice.BLL
{
   public class MyScheduleBLL
    {

        #region 根据日期查询日程
        /// <summary>
        /// 根据日期查询日程
        /// </summary>
        /// <param name="schDate">日期</param>
        /// <returns></returns>
        public List<v_DepUserSch> ShowSchedule(int userNo, DateTime schDate)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var date = from c in myoffice.v_DepUserSch
                           where c.SchDate == schDate && c.UserNo==userNo
                           select c;
                return date.ToList();
            };
        }  
        #endregion

        #region 查询所有日程
        /// <summary>
        /// 查询所有日程
        /// </summary>
        /// <returns></returns>
        public List<Schedule> ShowAllSchedule()
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var time = from c in myoffice.Schedules
                     
                           select c;
                return time.ToList();
            };
        }
        #endregion



        #region 根据日程编号查询所有日程
        /// <summary>
        /// 根据日程编号查询所有日程
        /// </summary>
        /// <returns></returns>
        public List<Schedule> ShowScheduleByNo(int schNo)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var time = from c in myoffice.Schedules
                           where c.SchNo==schNo
                           select c;
                return time.ToList();
            };
        }
        #endregion

        #region 根据用户编号和日期查询日程编号
        /// <summary>
        /// 根据日程编号查询所有日程
        /// </summary>
        /// <returns></returns>
        public int GetScheduleNo(int userNo,DateTime dt)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var time = from c in myoffice.v_DepUserSch
                           where c.UserNo== userNo && c.SchDate==dt
                           select c;

                List<v_DepUserSch> sch = time.ToList();
                if (sch.Count != 0)
                {
                    int schNo = sch[0].SchNo;
                    return schNo;
                }
                else
                    return 0;
              
            };
        }
        #endregion

        #region 添加日程
        /// <summary>
        /// 添加日程
        /// </summary>
        /// <param name="sch"></param>
        /// <param name="schDate"></param>
        /// <returns></returns>
        public bool AddSchedule(Schedule sch, DateTime schDate)
        {
            using (MyOfficeEntities myoffic = new MyOfficeEntities())
            {
                Schedule schedule = new Schedule()
                {
                    SchTopic = sch.SchTopic,//主题
                    SchAddr = sch.SchAddr,   //地址
                    SchCreateTime = sch.SchCreateTime,//创建时间
                    SchEndTime = sch.SchEndTime,//结束时间
                    SchContent = sch.SchContent,//内容
                    SchBeginTime = sch.SchBeginTime,//开始时间
                    SchIsPublish = sch.SchIsPublish,//是否公开
                    User = sch.User//创建者
                };
                schedule.MeetingType =myoffic.MeetingTypes.SingleOrDefault(m=>m.MeetingTypeName== sch.MeetingType.MeetingTypeName);//会议类型
                schedule.SchDate = schDate;
                using (MyOfficeEntities myoffice = new MyOfficeEntities())
                {
                    myoffice.Schedules.AddObject(schedule);
                    myoffice.SaveChanges();
                }
                if (sch.SchNo > 0)//返回是否台添加日程
                    return true;
                else
                    return false;
            }
        } 
        #endregion

        #region 修改日程
        /// <summary>
        /// 修改日程
        /// </summary>
        /// <param name="sch"></param>
        /// <param name="schDate"></param>
        /// <returns></returns>
        public bool UpdateSchedule(Schedule sch, DateTime schDate)
        {
            using (MyOfficeEntities myofffice = new MyOfficeEntities())
            {
                var update = from c in myofffice.Schedules
                             where c.SchDate == schDate
                             select c;        //查询该日期下的日程信息
                foreach (var item in update)
                {
                    item.SchTopic = sch.SchTopic;
                    item.SchAddr = sch.SchAddr;
                    item.SchBeginTime = sch.SchBeginTime;
                    item.SchEndTime = sch.SchEndTime;
                    item.SchIsPublish = sch.SchIsPublish;
                    item.SchCreateTime = sch.SchCreateTime;
                    item.SchContent = sch.SchContent;
                    item.MeetingType.MeetingTypeNo = sch.MeetingType.MeetingTypeNo;
                    item.User.UserId= sch.User.UserId;
                }
                int result = myofffice.SaveChanges();
                if (result > 0)    //返回是否修改日程
                    return true;
                else
                    return false;
            }
        } 
        #endregion

        #region 添加预约人
        /// <summary>
        /// 添加预约人
        /// </summary>
        /// <param name="subs"></param>
        /// <returns></returns>
        public bool AddPreContract(Subscribe subs)
        {
            Subscribe subscribe = new Subscribe();//实例化预约对象
            subscribe.Schedule.SchNo = subs.Schedule.SchNo;
            subscribe.User.UserNo = subs.User.UserNo; //预约的人

            using (MyOfficeEntities myoffic = new MyOfficeEntities())
            {
                myoffic.Subscribes.AddObject(subscribe);
                myoffic.SaveChanges();
            }
            if (subscribe.ScbscribeNo > 0)
                return true;
            else
                return false;
        } 
        #endregion

        #region 删除预约人
       /// <summary>
       /// 删除预约人
       /// </summary>
       /// <param name="userNo"></param>
       /// <returns></returns>
        public bool DeletePreContract(int userNo)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var user = from c in myoffice.Subscribes
                           where c.User.UserNo == userNo
                           select c;
                foreach (var item in user)
                {
                    myoffice.Subscribes.DeleteObject(item);//删除指定对象
                }
                int result = myoffice.SaveChanges();
                if (result > 0)
                    return true;
                else
                    return false;

            }

        } 
        #endregion

        #region 查询会议类型信息

        /// <summary>
        /// 查询会议类型信息
        /// </summary>
        /// <returns></returns>
        public List<MeetingType> GetMeetingType()
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var type = from c in myoffice.MeetingTypes
                           select c;
                return type.ToList();
            }
        } 
        #endregion

        #region 根据姓名、日期、部门查询日程信息 

        /// <summary>
        /// 根据姓名、日期、部门查询日程信息
        /// </summary>
        /// <param name="loginId"></param>
        /// <param name="userId"></param>
        /// <param name="depNo"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public List<v_DepUserSch> GetSchedule( string userId, string depNo, DateTime date)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var ds = from c in myoffice.v_DepUserSch
                         where c.UserId == userId && c.SchDate == date && c.DepId == depNo
                         select c;
                return ds.ToList();
            }
        } 
        #endregion

    }
}
